﻿using Microsoft.Xna.Framework;
using System.IO;
using Terraria;
using Terraria.GameContent.UI.Elements;
using Terraria.GameContent.UI.States;
using Terraria.Localization;
using Terraria.ModLoader;
using Terraria.ModLoader.IO;

namespace MasterMasterMode.Common.Systems
{
    public class MasterMasterModeSystem : ModSystem
    {      
        public bool MasterMasterMode;
        public override void LoadWorldData(TagCompound tag)
        {
            MasterMasterMode = tag.GetBool("MasterMasterMode");
        }
        public override void SaveWorldData(TagCompound tag)
        {
            tag["MasterMasterMode"] = MasterMasterMode;
        }
        public override void SaveWorldHeader(TagCompound tag)
        {
            tag["MasterMasterMode"] = MasterMasterMode;          
        }
        public override void NetReceive(BinaryReader reader)
        {
            reader.ReadFlags(out MasterMasterMode);
        }
        public override void NetSend(BinaryWriter writer)
        {
            writer.WriteFlags(MasterMasterMode);
        }
        public override void Load()
        {
            On_AWorldListItem.GetDifficulty += GetDifficultyOverride;
            On_UIWorldCreation.ProcessSpecialWorldSeeds += On_UIWorldCreation_ProcessSpecialWorldSeeds;
            On_UIWorldCreation.ProcessSeed += On_UIWorldCreation_ProcessSeed;

        }
        public override void Unload()
        {
            On_AWorldListItem.GetDifficulty -= GetDifficultyOverride;
            On_UIWorldCreation.ProcessSpecialWorldSeeds -= On_UIWorldCreation_ProcessSpecialWorldSeeds;
            On_UIWorldCreation.ProcessSeed -= On_UIWorldCreation_ProcessSeed;
        }
        private void GetDifficultyOverride(On_AWorldListItem.orig_GetDifficulty orig, AWorldListItem self, out string Text, out Color Color)
        {
            orig(self, out Text, out Color);
            bool MasterMaster = self.Data.TryGetHeaderData<MasterMasterModeSystem>(out TagCompound data);
            if (MasterMaster)
            {
                if (data.GetBool("MasterMasterMode"))
                {
                    Text = Language.GetTextValue("Mods.MasterMasterMode.UI.MasterMaster");
                    Color = Color.Red;
                }
            }
        }

        private void On_UIWorldCreation_ProcessSpecialWorldSeeds(On_UIWorldCreation.orig_ProcessSpecialWorldSeeds orig, string processedSeed)
        {
            orig.Invoke(processedSeed);
            MasterMasterMode = false;
            string seed = processedSeed.ToLower();
            if (seed == "mastermastermode" || seed == "master master mode" || seed == "Master Master Mode" || seed == "MasterMasterMode")
            {
                MasterMasterMode = true;
            }
        }
        private void On_UIWorldCreation_ProcessSeed(On_UIWorldCreation.orig_ProcessSeed orig, UIWorldCreation self, out string processedSeed)
        {
            orig.Invoke(self, out processedSeed);
            if (MasterMasterMode && Main.MenuUI.CurrentState is UIWorldCreation worldCreation)
            {
                processedSeed = Main.rand.Next().ToString();
            }
        }
    }
}